package com.mdd.system.provider;

import cn.dev33.satoken.SaManager;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mdd.system.api.SystemAuthAdminApi;
import com.mdd.system.entity.SystemAuthAdmin;
import com.mdd.system.entity.SystemAuthDept;
import com.mdd.system.mapper.SystemAuthAdminMapper;
import com.mdd.system.mapper.SystemAuthDeptMapper;
import com.mdd.system.params.SystemAdminParams;
import com.mdd.system.service.ISystemAuthAdminService;
import com.mdd.system.validate.SystemAdminCreateValidate;
import com.mdd.system.vo.SystemAuthAdminInformVo;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

/**
 * @Description
 * @Author 幻速
 * @Date 2024/3/19 14:09
 **/
@Service
public class SystemAuthAdminApiProvider implements SystemAuthAdminApi {
    @Resource
    SystemAuthAdminMapper systemAuthAdminMapper;
    @Resource
    SystemAuthDeptMapper systemAuthDeptMapper;
    @Resource
    ISystemAuthAdminService iSystemAuthAdminService;
    @Override
    public SystemAuthAdminInformVo getCurrentUser() {
        SystemAuthAdminInformVo systemAuthAdminInformVo=new SystemAuthAdminInformVo();
        Object adminId = SaManager.getStpLogic("admin").getLoginId();
        SystemAuthAdmin systemAuthAdmin = systemAuthAdminMapper.selectOne(
                new QueryWrapper<SystemAuthAdmin>()
                        .select("id,username,nickname, role_ids,dept_ids,post_ids,is_disable")
                        .eq("id", adminId)
                        .eq("is_delete", 0)
                        .last("limit 1"));
        SystemAuthDept systemAuthDept = systemAuthDeptMapper.selectById(systemAuthAdmin.getDeptIds());
        //获取当前登录用户
        systemAuthAdmin.setDeptCode(systemAuthDept.getDeptCode());
        BeanUtil.copyProperties(systemAuthAdmin,systemAuthAdminInformVo);
        return systemAuthAdminInformVo;
    }

    @Override
    public SystemAuthAdminInformVo findByUsername(String userName) {
        SystemAuthAdminInformVo systemAuthAdminInformVo=new SystemAuthAdminInformVo();
        SystemAuthAdmin systemAuthAdmin = iSystemAuthAdminService.findByUsername(userName);
        BeanUtil.copyProperties(systemAuthAdmin,systemAuthAdminInformVo);
        return systemAuthAdminInformVo;
    }

    @Override
    public void addAdminInfo(SystemAdminParams systemAdminParams,Long storeId) {
        //创建账号
        SystemAdminCreateValidate systemAdminCreateValidate = new SystemAdminCreateValidate();
        BeanUtil.copyProperties(systemAdminParams,systemAdminCreateValidate);
        iSystemAuthAdminService.add(systemAdminCreateValidate);
    }
}
